﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using bbtpClient.Entities;
using bbtpClient.Controllers;
using System.Text.RegularExpressions;

namespace bbtpClient
{
    public partial class CreateEditOrderForm : Form
    {
        private Order order;
        private Client client;
        private Good good;

        public CreateEditOrderForm( Order order, Client client, Good good)
        {
            InitializeComponent();
            label_UserName.Text = UserInfo.Name;
            textBox_orderName.Enabled = false;

            this.order = order;
            this.client = client;
            this.good = good;

            if (order.id == 0)
            {
                this.Text = "New order";
            }
            else
            {
                this.Text = "Edit order";
                SetDataToForm();
            }

        }

        private void SetDataToForm()
        {
            textBox_address.Text = order.address;
            textBox_quantityOfOrder.Text = order.quantity.ToString();

            if (good != null)
            {
                textBox_orderName.Text = good.name;
            }
            SetClient(client);
        }

        public void SetClient(Client client)
        {
            textBox_clientEmail.Text = client.email;
            textBox_clientName.Text = client.name;
            textBox_clientPhone.Text = client.phoneNumber;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool isEmailFormatCorrect(string email)
        {
            bool isEmailCorrect = false;
            if (!email.Equals(""))
            {
                string pattern = @"\w+\@\w+\.\w+";
                Match match = Regex.Match(email, pattern);
                if (match.Success)
                {
                    isEmailCorrect = true;
                }
            }
            return isEmailCorrect;
        }

        private void button_loadClientData_Click(object sender, EventArgs e)
        {
            string clientEmail = textBox_clientEmail.Text.Trim();
            SalesmanController.FindClient(clientEmail, this);
        }

        private void setOrderFromForm()
        {
            order.address = textBox_address.Text.Trim();
        }

        private void setClientFromForm()
        {
            client.email = textBox_clientEmail.Text.Trim();
            client.name = textBox_clientName.Text.Trim();
            client.phoneNumber = textBox_clientPhone.Text.Trim();
        }

        private bool IsClientDataCorrect()
        {
            bool isClientEmailCorrect = isEmailFormatCorrect(textBox_clientEmail.Text.Trim());
            bool isClientNameCorrect = false;
            bool isClientPhoneCorrect = false;

            if (!textBox_clientName.Text.Equals(""))
            {
                isClientNameCorrect = true;
            }

            if (!textBox_clientPhone.Text.Equals(""))
            {
                isClientPhoneCorrect = true;
            }

            return isClientEmailCorrect & isClientNameCorrect & isClientPhoneCorrect;
        }

        private bool IsDataCorrect(){
            bool isAddressCorrect = false;
            bool isGoodNameCorrect = false;
            bool isQuantityOfGoodCorrect = false;
            bool isClientDataCorrect = IsClientDataCorrect();

            if (!textBox_address.Text.Trim().Equals(""))
            {
                isAddressCorrect = true;
            }
            if (!textBox_orderName.Text.Trim().Equals(""))
            {
                isGoodNameCorrect = true;
            }
            if (!textBox_quantityOfOrder.Text.Trim().Equals(""))
            {
                isQuantityOfGoodCorrect = true;
            }
            return isAddressCorrect & isGoodNameCorrect & isQuantityOfGoodCorrect & isClientDataCorrect;
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            if (IsDataCorrect())
            {
                setOrderFromForm();
                setClientFromForm();
                SalesmanController.CreateOrder(order, client, this);
            }
            else
            {
                MessageBox.Show("Пожалуйста, заполните корректно все поля формы");
            }
        }

        public void SetGood(Good good)
        {
            order.goodId = good.id;
            this.good = good;
            textBox_orderName.Text = good.name;
            UpdateOrderSumLabel();
        }

        private void button_chooseGood_Click(object sender, EventArgs e)
        {
            SalesmanController.ShowGoodsDialog(this);
        }

        private void UpdateOrderSumLabel()
        {
            if (!textBox_quantityOfOrder.Text.Trim().Equals(""))
            {
                try
                {
                    order.quantity = Int32.Parse(textBox_quantityOfOrder.Text.Trim());
                    double sum = order.quantity * good.price;
                    label_orderSum.Text = sum.ToString();
                }
                catch(Exception e)
                {
                    MessageBox.Show("Введите количесво товара числом.");
                }
            }
        }

        private void textBox_quantityOfOrder_TextChanged(object sender, EventArgs e)
        {
            UpdateOrderSumLabel();
        }

        private void button_Cancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

   }
}